﻿
namespace Mulala.Cavan.Domain.MainBoundedContext.MulalaModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Core.Persistent;
    using Mulala.Cavan.Domain.MainBoundedContext.DictionaryModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Core.MyLinq;
    using Mulala.Cavan.Core.Extension;

    public class TagSpecification : ISpecification<Tag, TagSearch>
    {
        #region ISpecification<Tag,TagSearch> Members

        public TagSearch SearchParams
        {
            get;
            set;
        }

        #endregion

        #region ISpecification<Tag> Members

        public System.Linq.Expressions.Expression<Func<Tag, bool>> Expression
        {
            get
            {
                IQueryBuilder<Tag> queryBuilder = QueryBuilder.Create<Tag>();

                if (null != SearchParams)
                {
                    if (SearchParams.GroupType != null)
                    {
                        queryBuilder = queryBuilder.Equals(t => t.TagGroup.ID, SearchParams.GroupType.GetHashCode());
                    }
                    if (SearchParams.Value.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(t => t.Value, SearchParams.Value);
                    }
                    if (SearchParams.BinaryPosition >= 0)
                    {
                        queryBuilder = queryBuilder.Equals(t => t.BinaryPosition, SearchParams.BinaryPosition);
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }

        #endregion
    }
}
